<template>
	<view class="addressBox">

		<view class="addressList">
			<view class="addressitem" v-for="(item,index) in dataList">
				<view class="namephone" @click="handleSelectAddress(item)">
					<view class="name">
						{{item.name}}
					</view>
					<view class="phone">
						{{item.phone}}
					</view>
				</view>
				<view class="address" @click="handleSelectAddress(item)">
					{{item.address}}
				</view>
				<view class="heng"></view>
				<view class="bottom">
					<view class="bottomleft">
						<view v-if="item.check" class="selected" style="display: flex;"
							@click="handleceluserarea(item)">
							<image src="../../static/image/selected.png" mode="" style="width: 28rpx;height: 28rpx;">
							</image>
							<view class="" style="margin-left: 16rpx;">
								默认地址
							</view>
						</view>
						<view v-else class="" style="display: flex;" @click="handlesetuserarea(item)">
							<view class=""
								style="width: 25rpx;height: 25rpx;border-radius: 28rpx; border: 1rpx solid #A7A7A7;">
							</view>
							<view class="" style="margin-left: 16rpx;color: #A7A7A7;">
								默认地址
							</view>
						</view>
					</view>
					<view class="bottomright">
						<view class="bottomrightitem" @click="handleEdutAddress(item)">
							<image src="../../static/image/exit.png" mode="" style="width: 28rpx;height: 28rpx;">
							</image>
							<view class="" style="margin-left: 16rpx;">
								编辑
							</view>
						</view>
						<view class="bottomrightitem" style="margin-left: 64rpx;" @click="handleDelFlag(item)">
							<image src="../../static/image/delete.png" mode="" style="width: 28rpx;height: 28rpx;">
							</image>
							<view class="" style="margin-left: 16rpx;">
								删除
							</view>
						</view>
					</view>

				</view>

			</view>

		</view>



		<view class="bottomContent">
			<view class="btns" @click="handleAddaddress">
				新增地址
			</view>
		</view>


		<u-popup :show="delFlag" mode="center" :round="30">
			<view class="content">
				<view class="removetop">
					您确定要删除该地址吗？
				</view>
				<view class="heng" style="margin-top: 100rpx; margin-bottom: 0;">

				</view>
				<view class="removeBottom">
					<view class="item" style="border-right:2rpx solid #F7F8FA;" @click="delFlag = false">
						取消
					</view>
					<view class="item" style="color: #04767C;" @click="handledelArdess">
						确定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getuserarea,
		deluserarea,
		setuserarea,
		celuserarea
	} from "@/api/api.js"
	export default {
		data() {
			return {
				checked: false,
				dataList: [],
				delFlag: true,
				delFlag: false,
				addressId: "",
				orderId: null,
			}
		},
		onLoad(options) {
			if (!uni.getStorageSync("token")) {
				uni.reLaunch({
					url: "../login/index"
				})
				return
			}
			if (options.orderId) {
				this.orderId = options.orderId
			}
		},
		onShow() {
			this.handlegetuserarea()
		},
		methods: {
			async handlegetuserarea() {
				let res = await getuserarea()
				this.dataList = res.data
				this.dataList.forEach((item, index) => {
					item.check = item.type == '0' ? true : false
				})
				// console.log("地址列表", this.dataList)
			},

			handleCheck() {

			},
			handleDelFlag(item) {
				this.delFlag = true
				this.addressId = item.id
			},
			async handledelArdess() {
				let res = await deluserarea({
					id: this.addressId
				})
				if (res.code == 1000) {
					this.delFlag = false
					uni.showToast({
						title: "删除成功",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						this.handlegetuserarea()
					}, 2000)
				}
			},
			handleEdutAddress(item) {
				console.log("66", item)
				uni.setStorageSync("addressdetail", item.address)
				uni.navigateTo({
					url: `./addAddress?id=${item.id}`
				})
			},

			async handleceluserarea(item) {
				let res = await celuserarea({
					id: item.id
				})
				if (res.code == 1000) {
					uni.showToast({
						title: "取消成功",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						this.handlegetuserarea()
					}, 2000)
				}
			},

			async handlesetuserarea(item) {
				let res = await setuserarea({
					id: item.id
				})
				if (res.code == 1000) {
					uni.showToast({
						title: "设置成功",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						this.handlegetuserarea()
					}, 2000)
				}
			},

			handleSelectAddress(item) {
				item.orderId = this.orderId
				uni.setStorageSync("address", item)
				uni.navigateBack({
					delta: 1
				})
			},

			handleAddaddress() {
				uni.navigateTo({
					url: "./addAddress"
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 600rpx;
		height: 314rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 30rpx;
		box-sizing: border-box;

		.top {
			.title {
				width: 100%;
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 33rpx;
				text-align: center;
			}

			.text {
				margin-top: 20rpx;
				font-size: 36rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
				line-height: 42rpx;
				text-align: center;
			}
		}

		.heng {
			width: 560rpx;
			height: 2rpx;
			opacity: 1;
			background-color: #F7F8FA;
			margin: 40rpx 0;
		}

		.bottom {
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 38rpx;
			width: 100%;
			text-align: center;
		}

		.removetop {
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 42rpx;
			text-align: center;
			margin-top: 40rpx;
		}

		.removeBottom {
			display: flex;

			.item {
				width: 50%;
				text-align: center;
				height: 100rpx;
				line-height: 100rpx;
			}
		}
	}

	.addressBox {
		.addressList {
			padding: 30rpx;
			box-sizing: border-box;

			.addressitem {
				width: 100%;
				height: 264rpx;
				background: #FFFFFF;
				margin-bottom: 34rpx;
				padding: 30rpx 26rpx;
				box-sizing: border-box;

				.namephone {
					width: 100%;
					font-size: 28rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: 400;
					color: #333333;
					line-height: 28rpx;
					display: flex;
				}

				.address {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 400;
					color: #707070;
					line-height: 28rpx;
					margin-top: 34rpx;
				}

				.heng {
					width: 100%;
					height: 1rpx;
					background-color: #D8D8D8;
					margin-top: 30rpx;
				}

				.bottom {
					display: flex;
					justify-content: space-between;
					margin-top: 22rpx;
					font-size: 24rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 400;
					color: #359691;
					line-height: 28rpx;

					.bottomright {
						display: flex;

						.bottomrightitem {
							display: flex;
							align-items: center;
						}
					}
				}
			}
		}


		.bottomContent {
			width: 100%;
			background: #FFFFFF;
			position: fixed;
			bottom: 0;
			padding: 36rpx 30rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-bottom: constant(safe-area-inset-bottom);
			/* 兼容 iOS 设备 */
			padding-bottom: env(safe-area-inset-bottom);

			.btns {
				width: 638rpx;
				height: 72rpx;
				background: #04767C;
				border-radius: 276rpx 276rpx 276rpx 276rpx;
				font-size: 36rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				line-height: 72rpx;
				text-align: center;
				margin: 0 auto;
			}

		}

	}
</style>